package org.example.service.delay;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author Tang
 * @classname DelayConsumer
 * @description [  ]
 * @date 2023/4/14 08:58
 */
@Slf4j
@RabbitListener(queues = {"email.delay.queue"})
@Component
public class DelayConsumer {

    @RabbitHandler
    public void delayConsumer(String msg, @Headers Map<String, Object> headers, Channel channel) {
        log.info("Delay consumer-Receive: {}",msg);
        Long deliveryTag  = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}